package com.fresher.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.fresher.model.Accountinfo;
import com.fresher.model.User;

/**
 * DAO class for Account entity. This class contains all methods that
 * inserts/search/update account info
 * 
 *
 */
@Repository
public class AccountDao {

	/**
	 * @return all accounts from Account table
	 */

	@PersistenceContext
	private EntityManager entityManager;

	//get list accounts
	@Transactional
	public List<Accountinfo> getAccounts(String loginId) {

		// -----------Begin transaction-----------
		List<Accountinfo> AccountList = null;
		// Get a list of accounts from DB
		TypedQuery<User> query = entityManager.createQuery(
				"SELECT c FROM User c Where c.username= :para1", User.class);
		query.setParameter("para1", loginId);

		User user = (User) query.getSingleResult();
		AccountList = (List<Accountinfo>) user.getAccountinfos();
		// -----------End transaction-----------
		return AccountList;

	}

	// get balace
	@Transactional
	public int getBalance(Integer accountid) {

		// Obtains entity manager object

		// Obtains transaction from entity manager

		int balance = 0;
		// -----------Begin transaction-----------
		Accountinfo acc = new Accountinfo();

		// Get a list of accounts from DB
		Query query = entityManager.createQuery(
				"SELECT c FROM Accountinfo c Where c.idaccountInfo= :para1",
				Accountinfo.class);
		query.setParameter("para1", accountid);

		acc = (Accountinfo) query.getSingleResult();

		balance = acc.getBalance();

		// -----------End transaction-----------

		return balance;

	}

	

	// check account to id
	@Transactional
	public boolean checkAccount(int accountid) {
		Boolean exist = false;
		// -----------Begin transaction-----------
		Accountinfo acc = new Accountinfo();
		try {
			// Get a list of accounts from DB
			acc = entityManager.find(Accountinfo.class, accountid);
			if (acc != null) {
				exist = true;
			}
		} catch (Exception e) {
			entityManager.close();
		}
		return exist;

	}
	
//	// get account to id
//		@Transactional
//		public int getaccountTo(int accountid) {
//
//			// Obtains entity manager object
//
//			// Obtains transaction from entity manager
//
//			int id = 0;
//			// -----------Begin transaction-----------
//			Accountinfo acc = new Accountinfo();
//			try {
//				// Get a list of accounts from DB
//				Query query = entityManager
//						.createQuery(
//								"SELECT c FROM Accountinfo c Where c.idaccountInfo= :para1",
//								Accountinfo.class);
//				query.setParameter("para1", accountid);
//
//				acc = (Accountinfo) query.getSingleResult();
//
//				id = acc.getIdaccountInfo();
//			} catch (Exception e) {
//
//				entityManager.close();
//
//				id = -1;
//
//			}
//
//			// -----------End transaction-----------
//
//			return id;
//
//		}
//	// get account from id
//	@Transactional
//	public int getaccountFrom(int accountid) {
//
//		// Obtains entity manager object
//
//		// Obtains transaction from entity manager
//
//		int id = 0;
//		// -----------Begin transaction-----------
//		Accountinfo acc = new Accountinfo();
//		try {
//			// Get a list of accounts from DB
//			Query query = entityManager
//					.createQuery(
//							"SELECT c FROM Accountinfo c Where c.idaccountInfo= :para1",
//							Accountinfo.class);
//			query.setParameter("para1", accountid);
//
//			acc = (Accountinfo) query.getSingleResult();
//
//			id = acc.getIdaccountInfo();
//		} catch (Exception e) {
//
//			entityManager.close();
//
//			id = -1;
//
//		}
//		// -----------End transaction-----------
//
//		return id;
//
//	}
}
